import config from "../utils/config";

let taskStrck = [];
export class Task {
    constructor(asyncFunction,time = 10000){
        if(!time || !asyncFunction){
            throw new Error("error task ！");
        }
        this.time = time;
        this.asyncFunction = asyncFunction;
        this.isRuning = false;
    }
    async run(){
        if(this.isRuning){
            return;
        }
        await this.asyncFunction();
        setTimeout(() => {
            this.run();
        }, this.time);
    }
} 

function Runable(timeout = 1000){
    this.sleep = timeout;
}
Runable.prototype.start = function (){
    for(let trackItem of taskStrck){
        trackItem.run();
    }
}

export default Runable;
export function addTask(task){
    if(task instanceof Task){
        taskStrck.push(task);
    }
}
